package com.andy.nature.ui.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.andy.common.model.Label
import com.andy.common.model.Work
import com.andy.nature.databinding.AdapterRecruitItemBinding

/**
 * Author:Andy
 * Date:2023/3/20 19:25
 * Description:ArticleAdapter
 */
class RecruitAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    private var workList: MutableList<Work> = mutableListOf()
    private var labelList: MutableList<Label> = mutableListOf()
    private var listArray = mutableListOf<List<Work>>()
    fun setData(selectLabel: Label?, workList: List<Work>, labelList: List<Label>) {
        this.labelList.clear()
        this.labelList.addAll(labelList)
        listArray.clear()
        listArray.addAll(averageAssignFixLength(workList, 8))
        this.workList.clear()
        val count = listArray.size
        for (i in 0 until count) {
            val it = listArray[i]
            this.workList.addAll(it)
            val firstWork = Work()
            firstWork.id = "${System.currentTimeMillis()}$i"
            firstWork.name = "汇总"
            firstWork.selectLabel = selectLabel
            firstWork.workList = it
            this.workList.add(firstWork)
        }
        notifyDataSetChanged()
    }

    fun getLabelList(): MutableList<Label> {
        return labelList
    }

    fun getWorkList(): MutableList<Work> {
        return workList
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return ArticleViewHolder(
            AdapterRecruitItemBinding.inflate(
                LayoutInflater.from(parent.context), parent, false
            )
        )
    }

    override fun getItemCount(): Int {
        return workList.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val data = workList[position]
        (holder as ArticleViewHolder).binding.apply {
            if (data.name == "汇总") {
                tvTitle.isVisible = false
                tvPrice.isVisible = false
                rvLabelList.isVisible = false
                tvTotal.isVisible = true
                tvTitle.text = ""
                tvPrice.text = ""
                rvLabelList.apply {
                    layoutManager = LinearLayoutManager(this.context, LinearLayoutManager.HORIZONTAL, false)
                    val mAdapter = RecruitLabelAdapter()
                    adapter = mAdapter
                    mAdapter.setData(mutableListOf())
                }
            } else {
                tvTitle.isVisible = true
                tvPrice.isVisible = true
                rvLabelList.isVisible = true
                tvTotal.isVisible = false
                tvTitle.text = data.name
                tvPrice.text = "工价 ${data.labour}"
                rvLabelList.apply {
                    layoutManager = LinearLayoutManager(this.context, LinearLayoutManager.HORIZONTAL, false)
                    val mAdapter = RecruitLabelAdapter()
                    adapter = mAdapter
                    mAdapter.setData(assembleLabelList(data.label_arr))
                }
            }
            var index = (position + 1) % 9
            if (index == 0) {
                index = 9
            }
            tvIndex.text = "$index"
        }
    }


    internal class ArticleViewHolder(var binding: AdapterRecruitItemBinding) :
        RecyclerView.ViewHolder(binding.root)


    private fun assembleLabelList(ids: List<String>?): MutableList<Label> {
        if (ids.isNullOrEmpty()) {
            return mutableListOf()
        }
        val list = mutableListOf<Label>()
        labelList.forEach {
            if (ids.contains(it.id)) {
                list.add(it)
            }
        }
        return list
    }

    private fun averageAssignFixLength(source: List<Work>?, splitItemNum: Int): List<List<Work>> {
        val result = ArrayList<List<Work>>()
        if (source != null && source.run { isNotEmpty() } && splitItemNum > 0) {
            if (source.size <= splitItemNum) {
                // 源List元素数量小于等于目标分组数量
                result.add(source)
            } else {
                // 计算拆分后list数量
                val splitNum = if (source.size % splitItemNum == 0) source.size / splitItemNum else source.size / splitItemNum + 1
                var value: List<Work>? = null
                for (i in 0 until splitNum) {
                    value = if (i < splitNum - 1) {
                        source.subList(i * splitItemNum, (i + 1) * splitItemNum)
                    } else {
                        // 最后一组
                        source.subList(i * splitItemNum, source.size)
                    }
                    result.add(value)
                }
            }
        }
        return result
    }
}